package com.wwwarehouse.contract.contract;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.Animation;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.contract.bean.SettingRuleBean;
import com.wwwarehouse.contract.core.ContractConstant;
import com.wwwarehouse.contract.event.CountEvent;
import com.wwwarehouse.contract.event.DeleteEvent;
import com.wwwarehouse.contract.event.GoodsRefreshEvent;
import com.wwwarehouse.contract.event.PublishingRulesEvent;

import java.util.HashMap;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

/**
 * @author Joseph on 2017/10/19.
 *         <p/>
 *         设置发布规则
 */

public class ReleaseSettingPublishingRulesFragment extends MyBaseViewPagerFragment {

    private String pbUkid;
    private String sort;
    private Bundle bundle;
    private SettingRuleBean.DataBean dataBean;
    private String title = "设置发布规则";

    @Override
    public void setTitle() {
        if (peekFragment() instanceof ReleaseSettingPublishingRulesFragment) {
            mActivity.setTitle(title);
        }
    }

    @Override
    protected void loadDatas() {
        mConfirmBtn.setVisibility(View.VISIBLE);
        mConfirmBtn.setText(R.string.release);
        EventBus.getDefault().register(this);
        if (getArguments() != null) {
            pbUkid = getArguments().getString("pbUkid");
            sort = getArguments().getString("sort");
        }
        mActivity.setTitle(getString(R.string.set_release_rule));
        bundle = new Bundle();
        bundle.putString("pbUkid", pbUkid);
        bundle.putString("sort", sort);

        requestData();


        mConfirmBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Map<String, Object> map = new HashMap<>();
                map.put("pbUkid", pbUkid);
                httpPost(ContractConstant.PUBLISHRESOURCEITEM, map, 0, true, "");


            }
        });
    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }

    private void requestData() {
        Map<String, Object> map = new HashMap<>();
        map.put("page", "1"); //
        map.put("pbUkid", pbUkid); //  发布ukid
        map.put("size", bundle.getString("num")); //
        map.put("sort", sort); //     排序(DESC,ASC)
        httpPost(ContractConstant.GETPBRSITEMTEMPSBYPBUKID, map, 1, true, "");
    }

    @Override
    public void onSuccess(CommonClass commonClass, int what) {
        if (what == 0) {
            if (TextUtils.equals("0", commonClass.getCode())) {
                popFragment();
                popFragment();
                PublishingRulesEvent publishingRulesEvent = new PublishingRulesEvent(dataBean.getList());
                EventBus.getDefault().post(publishingRulesEvent);
            } else {
                toast("尚有资源未设置发布规则!");
            }
        } else if (what == 1) {
            if (TextUtils.equals("0", commonClass.getCode())) {
                dataBean = JSON.parseObject(commonClass.getData().toString(), SettingRuleBean.DataBean.class);
                EventBus.getDefault().post(new CountEvent(dataBean.getTotal()));
                if (dataBean.getTotal()!=0) {
                    title = getString(R.string.set_release_rule) + "(" + dataBean.getList().size() + ")";
                    mActivity.setTitle(title);
                    SetFragmentAndPage(dataBean.getTotal(), 5, new ReleaseSettingRulesGoodsListFragment().getClass().getName(), bundle);
                } else {
                    title = getString(R.string.set_release_rule);
                    mActivity.setTitle(title);
                    SetFragmentAndPage(dataBean.getTotal(), 5, new ReleaseSettingRulesGoodsListFragment().getClass().getName(), bundle);
                    mConfirmBtn.setVisibility(View.GONE);
                    PublishingRulesEvent publishingRulesEvent = new PublishingRulesEvent(dataBean.getList());
                    EventBus.getDefault().post(publishingRulesEvent);
                }
            } else {
                toast(commonClass.getMsg());
            }

        } else if (what == 2) {
            if (TextUtils.equals("0", commonClass.getCode())) {
                EventBus.getDefault().post(new GoodsRefreshEvent(""));
                requestData();

            } else {
                toast(commonClass.getMsg());
            }

        }

    }

    public void onEventMainThread(Object event) {
        if (event instanceof DeleteEvent) {
            Map<String, Object> map = new HashMap<>();
            map.put("itemTempUkid", ((DeleteEvent) event).getMsg()); //
            httpPost(ContractConstant.DELETEPBRSITEMTEMP, map, 2, true, "");

        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
